package com.halloon.android.ui.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.halloon.android.R;
import com.halloon.android.task.PostActionTask;
import com.halloon.android.util.Constants;

public class PublishFragment extends SherlockFragment implements OnClickListener {
	private Context context;
	
	private Button publishButton;
	private Button backButton;
	private EditText publishText;
	
	private ImageView imageAdd;
	private ImageView locationAdd;
	private ImageView atAdd;
	private ImageView sharpAdd;
	
	private String longitude;
	private String latitude;
	
	private boolean isImageAdd = false;
	private boolean isLocationAdd = false;
	
	private MyBroadCastReceiver myBroadCastReceiver = null;
	
	public interface PublishFragmentCallback{
		public void destroyPublishFragment();
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		this.context = activity;
		activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
	}
	
	@Override
	public void onStart(){
		super.onStart();
		Intent intent = new Intent();
		intent.setAction(Constants.GLOBAL_TAB_VISIBILITY);
		Bundle bundle = new Bundle();
		bundle.putBoolean("isTabShow", false);
		intent.putExtras(bundle);
		context.sendBroadcast(intent);
		
		myBroadCastReceiver = new MyBroadCastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constants.LOCATION_GOT_LOCATION);
		context.registerReceiver(myBroadCastReceiver, filter);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View root = inflater.inflate(R.layout.publish_popup, null);
		
		publishText = (EditText) root.findViewById(R.id.publish_text);
		publishButton = (Button) root.findViewById(R.id.publish_button);
		backButton = (Button) root.findViewById(R.id.back_button);
		imageAdd = (ImageView) root.findViewById(R.id.p_image);
		locationAdd = (ImageView) root.findViewById(R.id.p_location);
		atAdd = (ImageView) root.findViewById(R.id.p_at);
		sharpAdd = (ImageView) root.findViewById(R.id.p_sharp);
		
		imageAdd.setClickable(true);
		locationAdd.setClickable(true);
		atAdd.setClickable(true);
		sharpAdd.setClickable(true);
		
		publishButton.setOnClickListener(this);
		backButton.setOnClickListener(this);
		imageAdd.setOnClickListener(this);
		locationAdd.setOnClickListener(this);
		atAdd.setOnClickListener(this);
		sharpAdd.setOnClickListener(this);
		
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public void onStop(){
		super.onStop();
		Intent intent = new Intent();
		intent.setAction(Constants.GLOBAL_TAB_VISIBILITY);
		Bundle bundle = new Bundle();
		bundle.putBoolean("isTabShow", true);
		intent.putExtras(bundle);
		context.sendBroadcast(intent);
		context.unregisterReceiver(myBroadCastReceiver);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.publish_button:
			PostActionTask publishTask;
			if(!publishText.getText().equals("")){
				//ContentManager.getInstance(context).addText(publishText.getText().toString(), "127.0.0.1");
				if(isImageAdd && !isLocationAdd){
					publishTask = new PostActionTask(context, PostActionTask.PUBLISH_TWEET, publishText.getText().toString());
				}else if(isLocationAdd && !isImageAdd){
					publishTask = new PostActionTask(context, PostActionTask.PUBLISH_TWEET, publishText.getText().toString());
				}else if(isLocationAdd && isImageAdd){
					publishTask = new PostActionTask(context, PostActionTask.PUBLISH_TWEET, publishText.getText().toString());
				}else{
					publishTask = new PostActionTask(context, PostActionTask.PUBLISH_TWEET, publishText.getText().toString());
				}
				
				if(isLocationAdd && (longitude == null || latitude == null)){
					Toast.makeText(context, "正在等待定位", Toast.LENGTH_LONG).show();
				}else{
					if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
						publishTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
					}else{
						publishTask.execute();
					}
				}
			}else{
				Toast.makeText(context, context.getString(R.string.nothing_to_say), Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.back_button:
			((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			((PublishFragmentCallback) context).destroyPublishFragment();
			break;
		case R.id.p_image:
			if(isImageAdd){
				isImageAdd = false;
			}else{
				isImageAdd = true;
			}
			break;
		case R.id.p_location:
			if(isLocationAdd){
				isLocationAdd = false;
			}else{
				isLocationAdd = true;
			}
			break;
		case R.id.p_at:
			break;
		case R.id.p_sharp:
			break;
		}
	}
	
	private class MyBroadCastReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent){
			Bundle bundle = intent.getExtras();
			longitude = String.valueOf(bundle.getDouble("longitude"));
			latitude = String.valueOf(bundle.getDouble("latitude"));
		}
	}

}
